package com.itheima.HoneWork.homework01;

public class Demo01 {
    public static void main(String[] args) {
        MyThead myThead1 = new MyThead("窗口1");
        MyThead myThead2 = new MyThead("窗口2");
        MyThead myThead3 = new MyThead("窗口3");
        myThead1.start();
        myThead2.start();
        myThead3.start();
    }
}

class MyThead extends Thread {
    private static int count = 100;

    public MyThead(String name) {
        super(name);
    }

    @Override
    public void run() {
        while (true) {
            try {
                if (extracted()) {
                    break;
                }
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // 可以在这里添加更合理的处理逻辑，比如记录日志，结束线程等
                // 示例中简单地打印异常信息并结束线程
                System.err.println("线程 " + getName() + " 被中断，异常信息：" + e.getMessage());
                return;
            }
        }
    }

    private static synchronized boolean extracted() {
        if (count <= 0) {
            return true;
        }
        count--;
        System.out.println(Thread.currentThread().getName() + " 正在售票，剩余 " + count + " 张");
        return false;
    }
}